//
//  ECOrderMainViewController.m
//  ECERP.CRM
//
//  Created by 刘思麟 on 2018/11/8.
//  Copyright © 2018 Mac. All rights reserved.
//

#import "ECOrderMainViewController.h"
#import "ECButton.h"
#import "Charts-Swift.h"
#import "ECOrderBaseViewController.h"
#import "ECOrderTopShowEditViewController.h"
#import "ECMaterailManageHomeViewController.h"

#pragma mark - charts'formatters
/**
 最上面的barChartView的formatter
 */
@interface BarViewTopFormatter : NSObject<IChartAxisValueFormatter>

@property (nonatomic,strong) NSArray<NSString*> * titles;

@end

@implementation BarViewTopFormatter

- (instancetype)init {
    if (self = [super init]) {
        self.titles = @[@"q",@"w",@"e",@"r",@"t"];
    }
    return self;
}

- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis {
    return self.titles[[[NSNumber numberWithDouble:value] integerValue]];
}

@end

/**
 自上到下第二个barChartView的formatter
 */
@interface BarView2Formatter : NSObject<IChartAxisValueFormatter>

@property (nonatomic,strong) NSArray<NSString*> * titles;

@end

@implementation BarView2Formatter

- (instancetype)init {
    if (self = [super init]) {
        self.titles = @[@"a",@"b",@"c",@"d",@"e"];
    }
    return self;
}

- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis {
    return self.titles[[[NSNumber numberWithDouble:value] integerValue]];
}

@end

/**
 自上到下第三个barChartView的formatter
 */
@interface BarView3Formatter : NSObject<IChartAxisValueFormatter>

@property (nonatomic,strong) NSArray<NSString*> * titles;

@end

@implementation BarView3Formatter

- (instancetype)init {
    if (self = [super init]) {
        self.titles = @[@"z",@"x",@"c",@"v",@"b"];
    }
    return self;
}

- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis {
    return self.titles[[[NSNumber numberWithDouble:value] integerValue]];
}

@end


/**
 曲线图的Formatter
 */
@interface LineViewFormatter : NSObject<IChartAxisValueFormatter>

@property (nonatomic,strong) NSArray<NSString*> * titles;

@end

@implementation LineViewFormatter

- (instancetype)init {
    if (self = [super init]) {
        self.titles = @[@"y",@"u",@"i",@"o",@"p"];
    }
    return self;
}

- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis {
    return self.titles[[[NSNumber numberWithDouble:value] integerValue]];
}

@end


@interface ECOrderMainViewController ()<IChartValueFormatter> {
    NSMutableArray * kvoS;
    NSArray * orderStateArr;
    NSArray * orderTitle;
}

@property (nonatomic,strong) UIScrollView * scrollView;

@property (nonatomic,strong) UIView * topView;

@property (nonatomic,strong) NSMutableArray<NSMutableDictionary*> * topBTInfos;

@property (nonatomic,strong) NSArray<ECButton*> * topBTS;

@property (nonatomic,strong) UIView * top2View;

@property (nonatomic,strong) NSArray<ECButton*> * top2BTS;

@property (nonatomic,strong) UIView * incomeView;

@property (nonatomic,strong) UIView * orderView;

@property (nonatomic,strong) UIView * revenueView;

@end

@implementation ECOrderMainViewController
@synthesize topBTInfos = _topBTInfos;
- (void)viewDidLoad {
    [super viewDidLoad];
    kvoS = @[].mutableCopy;
    orderStateArr = @[@"0",@"1",@"2",@"3",@""];
    orderTitle = @[@"待审核订单",@"待发货订单",@"待复核订单",@"待收款订单",@"全部订单"];
    // Do any additional setup after loading the view.
}

- (void)loadSubView {
    WS(weakSelf, self);
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(weakSelf.view);
        if (@available(iOS 11.0,*)) {
            make.bottom.mas_equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        }else {
            make.bottom.mas_equalTo(weakSelf.view.mas_bottom);
        }
    }];
    
    [self.scrollView addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(weakSelf.scrollView);
        make.width.mas_equalTo(ECSCREEN_WIDTH);
        make.height.mas_equalTo(70 * PX_RELATIVE375);
    }];
    
    [self.scrollView addSubview:self.top2View];
    [self.top2View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.scrollView).mas_offset(12 * PX_RELATIVE375);
        make.top.mas_equalTo(weakSelf.topView.mas_bottom).mas_offset(10 * PX_RELATIVE375);
        make.width.mas_equalTo(ECSCREEN_WIDTH - 24 * PX_RELATIVE375);
        make.height.mas_equalTo(93 * PX_RELATIVE375);
    }];
    
    [self.scrollView addSubview:self.incomeView];
    [self.incomeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.scrollView);
        make.width.mas_equalTo(ECSCREEN_WIDTH);
        make.top.mas_equalTo(weakSelf.top2View.mas_bottom).mas_offset(10 * PX_RELATIVE375);
        make.height.mas_equalTo(280*PX_RELATIVE375);
    }];
    
    [self.scrollView addSubview:self.orderView];
    [self.orderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.scrollView);
        make.width.mas_equalTo(ECSCREEN_WIDTH);
        make.top.mas_equalTo(weakSelf.incomeView.mas_bottom).mas_offset(10 * PX_RELATIVE375);
        make.height.mas_equalTo(324*PX_RELATIVE375);
    }];
    
    [self.scrollView addSubview:self.revenueView];
    [self.revenueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.scrollView);
        make.width.mas_equalTo(ECSCREEN_WIDTH);
        make.top.mas_equalTo(weakSelf.orderView.mas_bottom).mas_offset(10 * PX_RELATIVE375);
        make.height.mas_equalTo(378*PX_RELATIVE375);
    }];
    
    [self reloadTopBTs];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.scrollView.contentSize = CGSizeMake(ECSCREEN_WIDTH, self.revenueView.bottom + 30);
}

#pragma mark -Action

- (ECButton*)creatBT:(NSString*)title image:(UIImage *)image {
    ECButton * bt = [ECButton buttonWithType:UIButtonTypeCustom];
    bt.imgLocation = ECButonImageLocationTop;
    bt.splitBetweenImgAndTitle = 5 * PX_RELATIVE375;
    [bt setTitleColor:ECCOLOR(0x666666) forState:UIControlStateNormal];
    bt.titleLabel.font = ECFONT(12 * PX_RELATIVE375);
    [bt setTitle:title forState:UIControlStateNormal];
    [bt setImage:image forState:UIControlStateNormal];
    return bt;
}

#pragma mark lazyLoad
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView  = [UIScrollView new];
        _scrollView.backgroundColor = ECCOLOR(0xf5f5f5);
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

- (UIView *)topView {
    if (!_topView) {
        _topView = [UIView new];
        _topView.backgroundColor = ECCOLOR(0xffffff);
//        WS(weakSelf, self);
//        WS(weakTopView, _topView);
//        __weak UIButton * cuBT = nil;
//        for (int index = 0; index < self.topBTS.count; index ++) {
//            UIButton * bt = weakSelf.topBTS[index];
//            [weakTopView addSubview:bt];
//            [bt mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.top.bottom.mas_equalTo(weakTopView);
//                make.width.mas_equalTo(ECSCREEN_WIDTH / weakSelf.topBTS.count);
//                if (index == 0) {
//                    make.left.mas_equalTo(weakTopView);
//                }else{
//                    make.left.mas_equalTo(cuBT.mas_right);
//                }
//            }];
//            cuBT = bt;
//        }
    }
    return _topView;
}

- (void)reloadTopBTs {
    WS(weakSelf, self);
    __weak UIButton * cuBT = nil;
    [self.topView removeAllSubviews];
    for (int index = 0; index < self.topBTS.count; index ++) {
        UIButton * bt = weakSelf.topBTS[index];
        [self.topView addSubview:bt];
        [bt mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_equalTo(weakSelf.topView);
            make.width.mas_equalTo(ECSCREEN_WIDTH / weakSelf.topBTS.count);
            if (index == 0) {
                make.left.mas_equalTo(weakSelf.topView);
            }else{
                make.left.mas_equalTo(cuBT.mas_right);
            }
        }];
        cuBT = bt;
    }
}

- (NSArray<UIButton *> *)topBTS {
    if (!_topBTS) {
        NSMutableArray * bts = @[].mutableCopy;
        for (int index = 0; index < self.topBTInfos.count;index ++) {
            NSMutableDictionary * muDic = self.topBTInfos[index];
            NSString * title = muDic[@"title"];
            ECButton * bt = [self creatBT:title image:[UIImage imageNamed:muDic[@"imgStr"]]];
            [bts addObject:bt];
            [bt addTarget:self action:@selector(topBTAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        _topBTS = bts.copy;
    }
    return _topBTS;
}

- (void)topBTAction:(ECButton *)sender {
    NSInteger index = [self.topBTS indexOfObject:sender];
    if (index == self.topBTS.count - 1) {
        ECOrderTopShowEditViewController * vc = [ECOrderTopShowEditViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index == 1){ //商品管理
        ECMaterailManageHomeViewController * vc = [[ECMaterailManageHomeViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (UIView *)top2View {
    if (!_top2View) {
        _top2View = [UIView new];
        _top2View.backgroundColor = ECCOLOR(0xffffff);
        WS(weakSelf, self);
        WS(weakTopView, _top2View);
        __weak UIButton * cuBT = nil;
        for (int index = 0; index < self.top2BTS.count; index ++) {
            UIButton * bt = weakSelf.top2BTS[index];
            bt.tag = 1000 + index;
            [bt addTarget:self action:@selector(jumpToOrder:) forControlEvents:UIControlEventTouchUpInside];
            [bt setTitleColor:ECCOLOR(0x999999) forState:UIControlStateNormal];
            [weakTopView addSubview:bt];
            [bt mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.mas_equalTo(weakTopView);
                make.width.mas_equalTo((ECSCREEN_WIDTH - 24 * PX_RELATIVE375) / weakSelf.top2BTS.count);
                if (index == 0) {
                    make.left.mas_equalTo(weakTopView);
                }else{
                    make.left.mas_equalTo(cuBT.mas_right);
                }
            }];
            cuBT = bt;
        }
    }
    return _top2View;
}

- (void)jumpToOrder:(UIButton*)sender {
    NSInteger tag = sender.tag;
    NSInteger orderStateIndex = tag - 1000;
    if (orderStateIndex >= orderStateArr.count) {
        return;
    }
    ECOrderBaseViewController * vc = [ECOrderBaseViewController new];
    vc.billState = orderStateArr[orderStateIndex];
    vc.navigationItem.title = orderTitle[orderStateIndex];
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSArray<ECButton *> *)top2BTS {
    if (!_top2BTS) {
        NSArray * btTitles = @[@"待审核",@"待发货",@"待复核",@"待收款",@"全部订单"];
        NSArray * imgStrs = @[@"order_待审核",@"order_待发货",@"order_待复核",@"order_待收款",@"order_全部订单"];
        NSMutableArray * bts = @[].mutableCopy;
        for (int index = 0; index < btTitles.count;index ++) {
            NSString * title = btTitles[index];
            ECButton * bt = [self creatBT:title image:[UIImage imageNamed:imgStrs[index]]];
            [bts addObject:bt];
        }
        _top2BTS = bts.copy;
    }
    return _top2BTS;
}

- (UIView *)incomeView {
    if (!_incomeView) {
        _incomeView = [UIView new];
        _incomeView.tag = 100;
        WS(weakView, _incomeView);
        _incomeView.backgroundColor = ECCOLOR(0xffffff);
        
        ECButton * timeStageBT = [ECButton buttonWithType:UIButtonTypeCustom];
        timeStageBT.tag = 101;
        [timeStageBT setTitleColor:ECCOLOR(0x999999) forState:UIControlStateNormal];
        [timeStageBT setTitle:@"2018.11.01-2018.11.05" forState:UIControlStateNormal];
        timeStageBT.titleLabel.font = ECFONT(12 * PX_RELATIVE375);
        timeStageBT.imgLocation = ECButonImageLocationRight;
        [timeStageBT setImage:[UIImage imageNamed:@"order_right"] forState:UIControlStateNormal];
        [_incomeView addSubview:timeStageBT];
        [timeStageBT mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20 * PX_RELATIVE375);
            make.right.mas_equalTo(weakView).mas_offset(-10* PX_RELATIVE375);
        }];
        
        WS(weakTimeBT, timeStageBT);
        ECButton * dateStageBT = [ECButton buttonWithType:UIButtonTypeCustom];
        dateStageBT.tag = 102;
        dateStageBT.titleLabel.font = ECFONT(12 * PX_RELATIVE375);
        dateStageBT.layer.cornerRadius = 11 * PX_RELATIVE375;
        dateStageBT.clipsToBounds = YES;
        dateStageBT.layer.borderWidth = 1 * PX_RELATIVE375;
        dateStageBT.layer.borderColor = ECCOLOR_NAVIBACK.CGColor;
        [dateStageBT setTitleColor:ECCOLOR(0x999999) forState:UIControlStateNormal];
        [dateStageBT setTitle:@"今日" forState:UIControlStateNormal];
        dateStageBT.imgLocation = ECButonImageLocationRight;
        dateStageBT.splitBetweenImgAndTitle = 5 * PX_RELATIVE375;
        [dateStageBT setImage:[UIImage imageNamed:@"order_bottom"] forState:UIControlStateNormal];
        [_incomeView addSubview:dateStageBT];
        [dateStageBT mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(weakTimeBT);
            make.right.mas_equalTo(weakTimeBT.mas_left).mas_offset(-10* PX_RELATIVE375);
            make.height.mas_equalTo(22 * PX_RELATIVE375);
            make.width.mas_equalTo(56 * PX_RELATIVE375);
        }];
        
        WS(weakDateBT, dateStageBT);
        UILabel * titleLB = [UILabel new];
        titleLB.adjustsFontSizeToFitWidth = YES;
        titleLB.font = ECFONT(16);
        titleLB.textColor = ECCOLOR(0x333333);
        titleLB.text = @"销售收入";
        [_incomeView addSubview:titleLB];
        [titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakView).mas_offset(16*PX_RELATIVE375);
            make.centerY.mas_equalTo(weakTimeBT);
            make.right.mas_lessThanOrEqualTo(weakDateBT.mas_left).mas_offset(5 * PX_RELATIVE375);
        }];
        
        WS(weakTitleLB, titleLB);
        UISegmentedControl * sege = [[UISegmentedControl alloc] initWithItems:@[@"柱状图",@"饼状图"]];
        sege.tag = 110;
        [sege addObserverBlockForKeyPath:@"selectedSegmentIndex" block:^(id  _Nonnull obj, id  _Nullable oldVal, id  _Nullable newVal) {
            switch ([newVal integerValue]) {
                case 0:
                    {
                        [weakView viewWithTag:111].hidden = NO;
                        [weakView viewWithTag:112].hidden = YES;
                        [(ChartViewBase*)[weakView viewWithTag:weakView.tag +11] animateWithXAxisDuration:2.0 yAxisDuration:2.0];
                    }
                    break;
                case 1:{
                    [weakView viewWithTag:111].hidden = YES;
                    [weakView viewWithTag:112].hidden = NO;
                    [(ChartViewBase*)[weakView viewWithTag:weakView.tag +12] animateWithXAxisDuration:2.0 yAxisDuration:2.0];
                }
                    break;
                default:
                    break;
            }
        }];
        [kvoS addObject:sege];
        sege.tintColor = ECCOLOR_NAVIBACK;
        [_incomeView addSubview:sege];
        [sege mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakView).mas_offset(15*PX_RELATIVE375);
            make.bottom.mas_equalTo(weakView).mas_offset(- 15*PX_RELATIVE375);
            make.width.mas_equalTo(90);
            make.height.mas_equalTo(20);
        }];
        
        WS(weakSege, sege);
        PieChartView * pieView = [self creatPieView];
        pieView.tag = 112;
        pieView.hidden = YES;
        [_incomeView addSubview:pieView];
        [pieView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(weakView);
            make.top.mas_equalTo(weakTitleLB.mas_bottom).mas_offset(5 * PX_RELATIVE375);
            make.bottom.mas_equalTo(weakSege.mas_top).mas_offset(-5 * PX_RELATIVE375);
        }];
        
        BarChartView * barView = [self creatBarChartView];
        barView.xAxis.valueFormatter = [BarViewTopFormatter new];
        [barView setData:[self barViewData:@[@[@(4),@(1),@(9),@(13),@(7)]] partLabels:@[@"订单前五"] partColors:@[ECCOLOR(0xF98A21)]]];
        barView.tag = 111;
        [_incomeView addSubview:barView];
        [barView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(weakView);
            make.top.mas_equalTo(weakTitleLB.mas_bottom).mas_offset(5 * PX_RELATIVE375);
            make.bottom.mas_equalTo(weakSege.mas_top).mas_offset(-5 * PX_RELATIVE375);
        }];
        
        ECButton * moreInfoBT = [ECButton buttonWithType:UIButtonTypeCustom];
        moreInfoBT.tag = 120;
        [moreInfoBT setTitleColor:ECCOLOR(0x999999) forState:UIControlStateNormal];
        [moreInfoBT setTitle:@"详情" forState:UIControlStateNormal];
        moreInfoBT.titleLabel.font = ECFONT(12 * PX_RELATIVE375);
        moreInfoBT.imgLocation = ECButonImageLocationRight;
        [moreInfoBT setImage:[UIImage imageNamed:@"order_right"] forState:UIControlStateNormal];
        [_incomeView addSubview:moreInfoBT];
        [moreInfoBT mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(weakSege);
            make.right.mas_equalTo(weakView).mas_offset(-10* PX_RELATIVE375);
        }];
        
        [sege setSelectedSegmentIndex:0];
    }
    return _incomeView;
}

- (UIView *)orderView {
    if (!_orderView) {
        _orderView = [UIView new];
        _orderView.tag = 200;
        WS(weakView, _orderView);
        _orderView.backgroundColor = ECCOLOR(0xffffff);
        
        ECButton * timeStageBT = [ECButton buttonWithType:UIButtonTypeCustom];
        timeStageBT.tag = _orderView.tag + 1;
        [timeStageBT setTitleColor:ECCOLOR(0x999999) forState:UIControlStateNormal];
        [timeStageBT setTitle:@"2018.11.01-2018.11.05" forState:UIControlStateNormal];
        timeStageBT.titleLabel.font = ECFONT(12 * PX_RELATIVE375);
        timeStageBT.imgLocation = ECButonImageLocationRight;
        [timeStageBT setImage:[UIImage imageNamed:@"order_right"] forState:UIControlStateNormal];
        [_orderView addSubview:timeStageBT];
        [timeStageBT mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20 * PX_RELATIVE375);
            make.right.mas_equalTo(weakView).mas_offset(-10* PX_RELATIVE375);
        }];
        
        WS(weakTimeBT, timeStageBT);
        ECButton * dateStageBT = [ECButton buttonWithType:UIButtonTypeCustom];
        dateStageBT.tag = _orderView.tag + 2;
        dateStageBT.titleLabel.font = ECFONT(12 * PX_RELATIVE375);
        dateStageBT.layer.cornerRadius = 11 * PX_RELATIVE375;
        dateStageBT.clipsToBounds = YES;
        dateStageBT.layer.borderWidth = 1 * PX_RELATIVE375;
        dateStageBT.layer.borderColor = ECCOLOR_NAVIBACK.CGColor;
        [dateStageBT setTitleColor:ECCOLOR(0x999999) forState:UIControlStateNormal];
        [dateStageBT setTitle:@"今日" forState:UIControlStateNormal];
        dateStageBT.imgLocation = ECButonImageLocationRight;
        dateStageBT.splitBetweenImgAndTitle = 5 * PX_RELATIVE375;
        [dateStageBT setImage:[UIImage imageNamed:@"order_bottom"] forState:UIControlStateNormal];
        [_orderView addSubview:dateStageBT];
        [dateStageBT mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(weakTimeBT);
            make.right.mas_equalTo(weakTimeBT.mas_left).mas_offset(-10* PX_RELATIVE375);
            make.height.mas_equalTo(22 * PX_RELATIVE375);
            make.width.mas_equalTo(56 * PX_RELATIVE375);
        }];
        
        WS(weakDateBT, dateStageBT);
        UILabel * titleLB = [UILabel new];
        titleLB.adjustsFontSizeToFitWidth = YES;
        titleLB.font = ECFONT(16);
        titleLB.textColor = ECCOLOR(0x333333);
        titleLB.text = @"客户订单统计";
        [_orderView addSubview:titleLB];
        [titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakView).mas_offset(16*PX_RELATIVE375);
            make.centerY.mas_equalTo(weakTimeBT);
            make.right.mas_lessThanOrEqualTo(weakDateBT.mas_left).mas_offset(5 * PX_RELATIVE375);
        }];
        
        WS(weakTitleLB, titleLB);
        UISegmentedControl * topSege = [[UISegmentedControl alloc] initWithItems:@[@"客户来源",@"客户类别"]];
        [kvoS addObject:topSege];
        topSege.tintColor = ECCOLOR_NAVIBACK;
        topSege.tag = _orderView.tag + 3;
        [topSege addObserverBlockForKeyPath:@"selectedSegmentIndex" block:^(id  _Nonnull obj, id  _Nullable oldVal, id  _Nullable newVal) {
            
        }];
        [_orderView addSubview:topSege];
        [topSege mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakView).mas_offset(15 * PX_RELATIVE375);
            make.width.mas_equalTo(ECSCREEN_WIDTH - 30 * PX_RELATIVE375);
            make.top.mas_equalTo(weakTitleLB.mas_bottom).mas_offset(20 * PX_RELATIVE375);
            make.height.mas_equalTo(24* PX_RELATIVE375);
        }];
        WS(weakTopSege, topSege);
        
        UISegmentedControl * sege = [[UISegmentedControl alloc] initWithItems:@[@"柱状图",@"饼状图"]];
        sege.tag = _orderView.tag + 10;
        [sege addObserverBlockForKeyPath:@"selectedSegmentIndex" block:^(id  _Nonnull obj, id  _Nullable oldVal, id  _Nullable newVal) {
            switch ([newVal integerValue]) {
                case 0:
                {
                    [weakView viewWithTag:weakView.tag + 11].hidden = NO;
                    [weakView viewWithTag:weakView.tag +12].hidden = YES;
                    [(ChartViewBase*)[weakView viewWithTag:weakView.tag +11] animateWithXAxisDuration:2.0 yAxisDuration:2.0];
                }
                    break;
                case 1:{
                    [weakView viewWithTag:weakView.tag +11].hidden = YES;
                    [weakView viewWithTag:weakView.tag +12].hidden = NO;
                    [(ChartViewBase*)[weakView viewWithTag:weakView.tag +12] animateWithXAxisDuration:2.0 yAxisDuration:2.0];
                }
                    break;
                default:
                    break;
            }
        }];
        [kvoS addObject:sege];
        sege.tintColor = ECCOLOR_NAVIBACK;
        [_orderView addSubview:sege];
        [sege mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakView).mas_offset(15*PX_RELATIVE375);
            make.bottom.mas_equalTo(weakView).mas_offset(- 15*PX_RELATIVE375);
            make.width.mas_equalTo(90);
            make.height.mas_equalTo(20);
        }];
        
        WS(weakSege, sege);
        PieChartView * pieView = [self creatPieView];
        pieView.tag = _orderView.tag + 12;
        pieView.hidden = YES;
        [_orderView addSubview:pieView];
        [pieView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(weakView);
            make.top.mas_equalTo(weakTopSege.mas_bottom).mas_offset(5 * PX_RELATIVE375);
            make.bottom.mas_equalTo(weakSege.mas_top).mas_offset(-5 * PX_RELATIVE375);
        }];
        
        BarChartView * barView = [self creatBarChartView];
        barView.xAxis.valueFormatter = [BarView2Formatter new];
        [barView setData:[self barViewData:@[@[@(12),@(1),@(5),@(11),@(9)]] partLabels:@[@"订单前五"] partColors:@[ECCOLOR(0xF98A21)]]];
        barView.tag = _orderView.tag + 11;
        [_orderView addSubview:barView];
        [barView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(weakView);
            make.top.mas_equalTo(weakTopSege.mas_bottom).mas_offset(5 * PX_RELATIVE375);
            make.bottom.mas_equalTo(weakSege.mas_top).mas_offset(-5 * PX_RELATIVE375);
        }];
        
        ECButton * moreInfoBT = [ECButton buttonWithType:UIButtonTypeCustom];
        moreInfoBT.tag = _orderView.tag + 20;
        [moreInfoBT setTitleColor:ECCOLOR(0x999999) forState:UIControlStateNormal];
        [moreInfoBT setTitle:@"详情" forState:UIControlStateNormal];
        moreInfoBT.titleLabel.font = ECFONT(12 * PX_RELATIVE375);
        moreInfoBT.imgLocation = ECButonImageLocationRight;
        [moreInfoBT setImage:[UIImage imageNamed:@"order_right"] forState:UIControlStateNormal];
        [_orderView addSubview:moreInfoBT];
        [moreInfoBT mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(weakSege);
            make.right.mas_equalTo(weakView).mas_offset(-10* PX_RELATIVE375);
        }];
        
        [sege setSelectedSegmentIndex:0];
        [topSege setSelectedSegmentIndex:0];
    }
    return _orderView;
}

- (UIView *)revenueView {
    if (!_revenueView) {
        _revenueView = [UIView new];
        _revenueView.tag = 300;
        WS(weakView, _revenueView);
        _revenueView.backgroundColor = ECCOLOR(0xffffff);
        
        ECButton * timeStageBT = [ECButton buttonWithType:UIButtonTypeCustom];
        timeStageBT.tag = _revenueView.tag + 1;
        [timeStageBT setTitleColor:ECCOLOR(0x999999) forState:UIControlStateNormal];
        [timeStageBT setTitle:@"2018.11.01-2018.11.05" forState:UIControlStateNormal];
        timeStageBT.titleLabel.font = ECFONT(12 * PX_RELATIVE375);
        timeStageBT.imgLocation = ECButonImageLocationRight;
        [timeStageBT setImage:[UIImage imageNamed:@"order_right"] forState:UIControlStateNormal];
        [_revenueView addSubview:timeStageBT];
        [timeStageBT mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20 * PX_RELATIVE375);
            make.right.mas_equalTo(weakView).mas_offset(-10* PX_RELATIVE375);
        }];
        
        WS(weakTimeBT, timeStageBT);
        ECButton * dateStageBT = [ECButton buttonWithType:UIButtonTypeCustom];
        dateStageBT.tag = _revenueView.tag + 2;
        dateStageBT.titleLabel.font = ECFONT(12 * PX_RELATIVE375);
        dateStageBT.layer.cornerRadius = 11 * PX_RELATIVE375;
        dateStageBT.clipsToBounds = YES;
        dateStageBT.layer.borderWidth = 1 * PX_RELATIVE375;
        dateStageBT.layer.borderColor = ECCOLOR_NAVIBACK.CGColor;
        [dateStageBT setTitleColor:ECCOLOR(0x999999) forState:UIControlStateNormal];
        [dateStageBT setTitle:@"今日" forState:UIControlStateNormal];
        dateStageBT.imgLocation = ECButonImageLocationRight;
        dateStageBT.splitBetweenImgAndTitle = 5 * PX_RELATIVE375;
        [dateStageBT setImage:[UIImage imageNamed:@"order_bottom"] forState:UIControlStateNormal];
        [_revenueView addSubview:dateStageBT];
        [dateStageBT mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(weakTimeBT);
            make.right.mas_equalTo(weakTimeBT.mas_left).mas_offset(-10* PX_RELATIVE375);
            make.height.mas_equalTo(22 * PX_RELATIVE375);
            make.width.mas_equalTo(56 * PX_RELATIVE375);
        }];
        
        WS(weakDateBT, dateStageBT);
        UILabel * titleLB = [UILabel new];
        titleLB.adjustsFontSizeToFitWidth = YES;
        titleLB.font = ECFONT(16);
        titleLB.textColor = ECCOLOR(0x333333);
        titleLB.text = @"营收趋势";
        [_revenueView addSubview:titleLB];
        [titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakView).mas_offset(16*PX_RELATIVE375);
            make.centerY.mas_equalTo(weakTimeBT);
            make.right.mas_lessThanOrEqualTo(weakDateBT.mas_left).mas_offset(5 * PX_RELATIVE375);
        }];
        
        WS(weakTitleLB, titleLB);
        UISegmentedControl * topSege = [[UISegmentedControl alloc] initWithItems:@[@"订单金额",@"收款金额",@"订单量"]];
        [kvoS addObject:topSege];
        topSege.tintColor = ECCOLOR_NAVIBACK;
        topSege.tag = _revenueView.tag + 3;
        [topSege addObserverBlockForKeyPath:@"selectedSegmentIndex" block:^(id  _Nonnull obj, id  _Nullable oldVal, id  _Nullable newVal) {
            
        }];
        [_revenueView addSubview:topSege];
        [topSege mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakView).mas_offset(15 * PX_RELATIVE375);
            make.width.mas_equalTo(ECSCREEN_WIDTH - 30 * PX_RELATIVE375);
            make.top.mas_equalTo(weakTitleLB.mas_bottom).mas_offset(20 * PX_RELATIVE375);
            make.height.mas_equalTo(24* PX_RELATIVE375);
        }];
        WS(weakTopSege, topSege);
        
        UISegmentedControl * sege = [[UISegmentedControl alloc] initWithItems:@[@"柱状图",@"曲线图"]];
        sege.tag = _revenueView.tag + 10;
        [sege addObserverBlockForKeyPath:@"selectedSegmentIndex" block:^(id  _Nonnull obj, id  _Nullable oldVal, id  _Nullable newVal) {
            switch ([newVal integerValue]) {
                case 0:
                {
                    [weakView viewWithTag:weakView.tag + 11].hidden = NO;
                    [weakView viewWithTag:weakView.tag +12].hidden = YES;
                    [(ChartViewBase*)[weakView viewWithTag:weakView.tag +11] animateWithXAxisDuration:2.0 yAxisDuration:2.0];
                }
                    break;
                case 1:{
                    [weakView viewWithTag:weakView.tag +11].hidden = YES;
                    [weakView viewWithTag:weakView.tag +12].hidden = NO;
                    [(ChartViewBase*)[weakView viewWithTag:weakView.tag +12] animateWithXAxisDuration:2.0 yAxisDuration:2.0];
                }
                    break;
                default:
                    break;
            }
        }];
        [kvoS addObject:sege];
        sege.tintColor = ECCOLOR_NAVIBACK;
        [_revenueView addSubview:sege];
        [sege mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakView).mas_offset(15*PX_RELATIVE375);
            make.bottom.mas_equalTo(weakView).mas_offset(- 15*PX_RELATIVE375);
            make.width.mas_equalTo(90);
            make.height.mas_equalTo(20);
        }];
        
        WS(weakSege, sege);
        BarChartView * barView = [self creatBarChartView];
        barView.xAxis.valueFormatter = [BarView3Formatter new];
        [barView setData:[self barViewData:@[@[@(12),@(1),@(5),@(11),@(9)],
  @[@(4),@(1),@(9),@(13),@(7)],
  @[@(6),@(2),@(4),@(11),@(5)]] partLabels:@[@"qq",@"ww",@"ee"] partColors:@[ECCOLOR(0xF98A21),ECCOLOR(0xF98A21),ECCOLOR(0xF98A21)]]];
        barView.tag = _revenueView.tag + 11;
        barView.hidden = YES;
        [_revenueView addSubview:barView];
        [barView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(weakView);
            make.top.mas_equalTo(weakTopSege.mas_bottom).mas_offset(5 * PX_RELATIVE375);
            make.bottom.mas_equalTo(weakSege.mas_top).mas_offset(-5 * PX_RELATIVE375);
        }];
        
        LineChartView * lineView = [self creatLineChartView];
        lineView.xAxis.valueFormatter = [LineViewFormatter new];
        lineView.tag = _revenueView.tag + 12;
        [_revenueView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(weakView);
            make.top.mas_equalTo(weakTopSege.mas_bottom).mas_offset(5 * PX_RELATIVE375);
            make.bottom.mas_equalTo(weakSege.mas_top).mas_offset(-5 * PX_RELATIVE375);
        }];
        
        ECButton * moreInfoBT = [ECButton buttonWithType:UIButtonTypeCustom];
        moreInfoBT.tag = _revenueView.tag + 20;
        [moreInfoBT setTitleColor:ECCOLOR(0x999999) forState:UIControlStateNormal];
        [moreInfoBT setTitle:@"详情" forState:UIControlStateNormal];
        moreInfoBT.titleLabel.font = ECFONT(12 * PX_RELATIVE375);
        moreInfoBT.imgLocation = ECButonImageLocationRight;
        [moreInfoBT setImage:[UIImage imageNamed:@"order_right"] forState:UIControlStateNormal];
        [_revenueView addSubview:moreInfoBT];
        [moreInfoBT mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(weakSege);
            make.right.mas_equalTo(weakView).mas_offset(-10* PX_RELATIVE375);
        }];
        
        [sege setSelectedSegmentIndex:0];
        [topSege setSelectedSegmentIndex:0];
    }
    return _revenueView;
}

- (PieChartView*)creatPieView {
    PieChartView * pieView = [[PieChartView alloc] init];
    [pieView setExtraOffsetsWithLeft:0 top:10 right:0 bottom:10];
    pieView.backgroundColor = ECCOLOR(0xffffff);
    pieView.dragDecelerationEnabled = YES;
    pieView.drawCenterTextEnabled = YES;
    pieView.usePercentValuesEnabled = YES;
    pieView.drawHoleEnabled = YES;
    pieView.holeRadiusPercent = 0.6;
    pieView.holeColor = ECCOLOR(0xffffff);
    [pieView animateWithXAxisDuration:0.25];
    [pieView animateWithYAxisDuration:0.25];
    [pieView setData:[self pieData:@[@(5),@(3),@(11),@(4),@(2)] lables:@[@"1",@"2",@"3",@"4",@"5"]]];
    [pieView.legend setDrawInside:NO];
    pieView.legend.enabled = NO;
    return pieView;
}

- (BarChartView * )creatBarChartView {
    BarChartView * barChartView = [[BarChartView alloc] init];
    [barChartView setExtraOffsetsWithLeft:0 top:10 right:0 bottom:0];
    barChartView.backgroundColor = ECCOLOR(0xffffff);
    barChartView.legend.form = ChartLegendFormCircle;
    barChartView.legend.textColor = ECCOLOR(0xF98A21);
    barChartView.legend.yOffset = 10*PX_RELATIVE375;
    barChartView.legend.verticalAlignment = ChartLegendVerticalAlignmentTop;
    barChartView.legend.horizontalAlignment = ChartLegendHorizontalAlignmentRight;
    ChartXAxis *xAxis = barChartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.drawGridLinesEnabled = NO;
    xAxis.labelCount=5;
    barChartView.leftAxis.enabled = NO;
    barChartView.rightAxis.enabled = NO;
    barChartView.drawValueAboveBarEnabled = YES;
    return barChartView;
}

- (LineChartView*)creatLineChartView {
    LineChartView * chartView = [[LineChartView alloc] init];
    [chartView setExtraOffsetsWithLeft:10 top:10 right:10 bottom:0];
    chartView.backgroundColor = ECCOLOR(0xffffff);
    chartView.legend.form = ChartLegendFormCircle;
    chartView.legend.textColor = ECCOLOR(0xF98A21);
    chartView.legend.yOffset = 10*PX_RELATIVE375;
    chartView.legend.verticalAlignment = ChartLegendVerticalAlignmentTop;
    chartView.legend.horizontalAlignment = ChartLegendHorizontalAlignmentRight;
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.drawAxisLineEnabled = YES;
    xAxis.labelCount=4;
    xAxis.axisMinimum = - 0.5;
    xAxis.axisMaximum = 4.5;
    chartView.leftAxis.enabled = NO;
    chartView.rightAxis.enabled = NO;

    [chartView setData:[self lineViewData]];
    return chartView;
}

- (LineChartData *)lineViewData {
    int count = 5;
    int range = 60;
    
    NSMutableArray * sets = @[].mutableCopy;
    for (int index = 0;index < 3;index ++ ){
        NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
        for (int i = 0; i < count; i++)
        {
            double mult = (range + 1);
            double val = (double) (arc4random_uniform(mult)) + 20;
            [yVals1 addObject:[[ChartDataEntry alloc] initWithX:i y:val]];
        }
        
        LineChartDataSet *set1 = nil;
        set1 = [[LineChartDataSet alloc] initWithValues:yVals1 label:@"DataSet 1"];
        set1.mode = LineChartModeCubicBezier;
        set1.cubicIntensity = 0.2;
        set1.drawCirclesEnabled = YES;
        set1.lineWidth = 1.8;
        set1.circleRadius = 2.0;
        [set1 setCircleColor:ECCOLOR(0x2585e5)];
        set1.highlightColor = ECCOLOR(0xF98A21);
        [set1 setColor:ECCOLOR(0xF98A21)];
        set1.drawHorizontalHighlightIndicatorEnabled = NO;
        [sets addObject:set1];
    }
    
    LineChartData *data = [[LineChartData alloc] initWithDataSets:sets];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:9.f]];
    [data setDrawValues:NO];
    
    return data;
}

- (BarChartData*)barViewData:(NSArray<NSArray<NSNumber*>*>*)datas partLabels:(NSArray<NSString*>*)partLabels partColors:(NSArray<UIColor*>*)partColors{
    
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    
    for (int index = 0; index <datas.count; index ++) {
        NSMutableArray *yVals = [[NSMutableArray alloc] init];
        NSArray<NSNumber*>* data = datas[index];
        for(int i =0; i < data.count; i++) {
            CGFloat val = [data[i] doubleValue];
            [yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:val]];
        }
        BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithValues:yVals label:partLabels[index]];
        [set1 setColor:partColors[index]];//bar的颜色
        [set1 setHighlightEnabled:NO];
        [dataSets addObject:set1];
    }
    
    BarChartData*data = [[BarChartData alloc]initWithDataSets:dataSets];
    [data setBarWidth:0.1];
    [data setDrawValues:YES];
    [data setValueFont:ECFONT(10)];
    [data setValueTextColor:ECCOLOR(0x333333)];
    
    [data groupBarsFromX: -0.48 groupSpace: 0.63 barSpace: 0.02f];
    
    return data;
}

- (PieChartData *)pieData:(NSArray<NSNumber*>*)datas lables:(NSArray<NSString*>*)labels{
    //每个区块的数据
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    for (int index = 0; index < datas.count; index ++) {
        PieChartDataEntry *entry = [[PieChartDataEntry alloc] initWithValue:[datas[index] doubleValue] label:labels[index]];
        [yVals addObject:entry];
    }
    
    //dataSet
    PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithValues:yVals label:@""];
    dataSet.drawValuesEnabled = YES;//是否绘制显示数据
    NSMutableArray *colors = [[NSMutableArray alloc] init];
    [colors addObjectsFromArray:ChartColorTemplates.vordiplom];
    [colors addObjectsFromArray:ChartColorTemplates.joyful];
    [colors addObjectsFromArray:ChartColorTemplates.colorful];
    [colors addObjectsFromArray:ChartColorTemplates.liberty];
    [colors addObjectsFromArray:ChartColorTemplates.pastel];
    [colors addObject:[UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]];
    dataSet.colors = colors;//区块颜色
    dataSet.sliceSpace = 0;//相邻区块之间的间距
    dataSet.selectionShift = 8;//选中区块时, 放大的半径
    dataSet.xValuePosition = PieChartValuePositionOutsideSlice;//名称位置
    dataSet.yValuePosition = PieChartValuePositionOutsideSlice;//数据位置
    //数据与区块之间的用于指示的折线样式
    dataSet.valueLinePart1OffsetPercentage = 0.85;//折线中第一段起始位置相对于区块的偏移量, 数值越大, 折线距离区块越远
    dataSet.valueLinePart1Length = 0.4;//折线中第一段长度占比
    dataSet.valueLinePart2Length = 0.4;//折线中第二段长度最大占比
    dataSet.valueLineWidth = 1;//折线的粗细
    dataSet.valueLineColor = [UIColor brownColor];//折线颜色
    
    //data
    PieChartData *data = [[PieChartData alloc] initWithDataSet:dataSet];
//    [data setValueFormatter:self];//设置显示数据显示代理
    [data setValueTextColor:[UIColor brownColor]];
    [data setValueFont:[UIFont systemFontOfSize:10]];
    
    return data;
}

#pragma mark IChartValueFormatter

- (NSString *)stringForValue:(double)value entry:(ChartDataEntry *)entry dataSetIndex:(NSInteger)dataSetIndex viewPortHandler:(ChartViewPortHandler *)viewPortHandler {
    return @"asdd";
}

- (NSMutableArray<NSMutableDictionary *> *)topBTInfos {
    if (!_topBTInfos) {
        _topBTInfos = @[].mutableCopy;
        NSArray * btTitles = @[@"报价",@"商品管理",@"销售退货历史",@"采购退货历史",@"添加"];
        NSArray * imgStrs = @[@"order_报价",@"order_商品管理",@"order_销售退货",@"order_采购退货",@"order_add"];
        NSArray * jumpVCS = @[];
        for (int index = 0; index < btTitles.count; index ++) {
            NSMutableDictionary * muDic = @{@"title":btTitles[index],@"imgStr":imgStrs[index]}.mutableCopy;
            [_topBTInfos addObject:muDic];
        }
    }
    return _topBTInfos;
}

- (void)setTopBTInfos:(NSMutableArray<NSMutableDictionary *> *)topBTInfos {
    [topBTInfos appendObject:@{@"title":@"添加",@"imgStr":@"order_add"}];
    _topBTInfos = topBTInfos;
}

#pragma mark kvo

- (void)dealloc {
    [kvoS enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeObserverBlocks];
    }];
}

@end
